#include <iostream>
#include <mutex>
#include <thread>

using std::cout;
using std::endl;
using std::mutex;
using std::thread;
using std::unique_lock;

mutex mtx;//创建了锁的对象

int gCnt = 0;

void threadFunc()
{
    for(size_t idx = 0; idx != 1000000; ++idx)
    {
        //使用了RAII的思想，利用栈对象的生命周期管理资源。
        unique_lock<mutex> ul(mtx);//lg栈对象
        ++gCnt;
        ul.unlock();
        //xxx  不加锁
        ul.lock();
        //yyy  加锁
        ul.unlock();
    }
}

int main(int argc, char *argv[])
{
    thread thA(threadFunc);
    thread thB(threadFunc);

    thA.join();
    thB.join();

    cout << "gCnt = " << gCnt << endl;
    return 0;
}

